﻿// -----------------------------------------------------------------------
// <copyright file="CustomList.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.ControlBuilders
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using Metal.Config;
    using System.Drawing;
    using Gui.ThemeEngine;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CustomList : BaseControl
    {
        /// <summary>
        /// Create the custom control
        /// </summary>
        /// <param name="controlProperties">The Xml node that contains the control properties</param>
        /// <returns>Returns custom control</returns>
        public Controls.MetalList GetControl(XmlNode controlProperties)
        {
            string strThemeName = Settings.Instance.GetValue("LastSettings.Theme", "BlackMetal");

            Controls.MetalList customList = new Controls.MetalList();

            this.BuildControl(controlProperties, customList);
            
            customList.BackgroundColor = PropertyProvider.GetValue(controlProperties, "backgroundColor");

            customList.BackgroundImage = System.Drawing.Image.FromFile(Paths.ThemesFolder + strThemeName + "\\" + PropertyProvider.GetValue(controlProperties, "backgroundImage", "no-image.png"));

            // Set font
            FontStyle styleMenuItem = (FontStyle)Enum.Parse(typeof(FontStyle), PropertyProvider.GetValue(controlProperties, "fontStyle", "Regular"));
            customList.SetFont(PropertyProvider.GetValue(controlProperties, "font", "Default"), PropertyProvider.GetValue(controlProperties, "fontSize", 10), styleMenuItem);

            customList.CommandIdentifier = PropertyProvider.GetValue(controlProperties, "command", "InvalidIdentifier");

            if (PropertyProvider.GetValue(controlProperties, "showSelectedRow", "False") == "True")
            {
                customList.ShowSelectedRow = true;
            }

            // Hide background when scrolling the list (due to some flickers on complex backgrounds). Default, is false.
            customList.HideBackgroundOnScroll = PropertyProvider.GetValue(controlProperties, "hideBackgroundOnScroll", false);

            // Set rows template
            customList.RowsDefaultCellStyle.SelectionBackColor = PropertyProvider.GetValue(controlProperties, "selectionBackgroundColor");
            customList.RowsDefaultCellStyle.SelectionForeColor = PropertyProvider.GetValue(controlProperties, "selectionForeColor");
            customList.RowsDefaultCellStyle.ForeColor = PropertyProvider.GetValue(controlProperties, "foreColor");
            customList.RowsDefaultCellStyle.BackColor = PropertyProvider.GetValue(controlProperties, "backgroundColor");

            customList.CustomRowForegroundColor = PropertyProvider.GetValue(controlProperties, "foreColor");
            customList.CustomRowSelectedForegroundColor = PropertyProvider.GetValue(controlProperties, "selectionForeColor");
            customList.CustomRowGradientSecondColor = PropertyProvider.GetValue(controlProperties, "selectionGradientSecondColor");
            customList.CustomRowSelectedBackColor = PropertyProvider.GetValue(controlProperties, "selectionBackgroundColor");

            customList.CustomSecondRowForegroundColor = PropertyProvider.GetValue(controlProperties, "secondRowForeColor");
            customList.CustomSecondRowSelectedForegroundColor = PropertyProvider.GetValue(controlProperties, "secondRowSelectedForeColor");

            // Set default properties
            customList.ColumnHeadersVisible = false;
            customList.RowHeadersVisible = false;
            customList.EditMode = DataGridViewEditMode.EditProgrammatically;
            customList.ScrollBars = ScrollBars.Vertical;
            customList.AllowUserToResizeRows = false;
            customList.RowHeight = PropertyProvider.GetValue(controlProperties, "rowHeight", 9);

            if (PropertyProvider.GetValue(controlProperties, "style", "Simple") == "Simple")
            {
                customList.Style = Controls.MetalList.ListStyle.Simple;
            }
            else
            {
                customList.Style = Controls.MetalList.ListStyle.Details;
            }

            if (PropertyProvider.GetValue(controlProperties, "resizeColumns", "True") == "False")
            {
                customList.AllowUserToResizeColumns = false;
            }

            // Add columns
            if (PropertyProvider.GetValue(controlProperties, "showColumnsHeader", string.Empty) == "True")
            {
                customList.ColumnHeadersVisible = true;

                // Set columns template
                customList.ColumnHeadersDefaultCellStyle.ForeColor = PropertyProvider.GetValue(controlProperties, "columnForeColor");
                customList.ColumnHeadersDefaultCellStyle.BackColor = PropertyProvider.GetValue(controlProperties, "columnBackColor");
            }

            customList.ListDataSet = new System.Data.DataSet();
            customList.ListDataSet.Tables.Add().Namespace = "List";
            customList.ListDataSet.Tables[0].TableName = "Row";
            customList.ListDataSet.DataSetName = "Playlist";

            XmlNode columnsNode = controlProperties.SelectSingleNode("Columns");
            if (columnsNode != null)
            {
                customList.VisibleColumns = new bool[columnsNode.ChildNodes.Count];
                foreach (XmlNode column in columnsNode.ChildNodes)
                {
                    int index = Convert.ToInt32(PropertyProvider.GetValue(column, "index", "0"));

                    customList.ListDataSet.Tables[0].Columns.Add().Namespace = PropertyProvider.GetValue(column, "name", string.Empty);
                    customList.ListDataSet.Tables[0].Columns[index].ColumnName = PropertyProvider.GetValue(column, "name", string.Empty);

                    if (PropertyProvider.GetValue(column, "typeOf", string.Empty) == "int")
                    {
                        customList.ListDataSet.Tables[0].Columns[index].DataType = typeof(int);
                    }
                    else if (PropertyProvider.GetValue(column, "typeOf", string.Empty) == "string")
                    {
                        customList.ListDataSet.Tables[0].Columns[index].DataType = typeof(string);
                    }

                    if (PropertyProvider.GetValue(column, "visible", string.Empty) == "True")
                    {
                        customList.VisibleColumns[Convert.ToInt32(PropertyProvider.GetValue(column, "index", "0"))] = true;
                    }
                    else
                    {
                        customList.VisibleColumns[Convert.ToInt32(PropertyProvider.GetValue(column, "index", "0"))] = false;
                    }
                }
            }

            // Add list scrollbar (if exists)
            XmlNode scrollbarNode = controlProperties.SelectSingleNode("Scrollbar");
            if (scrollbarNode != null)
            {
                // Remove the default scrollbar and add our nice looking one :)
                customList.ScrollBars = ScrollBars.None;

                ControlBuilder builder = new ControlBuilder();
                customList.Controls.Add(builder.CreateControl(scrollbarNode));
            }

            return customList;
        }

        /// <summary>
        /// Apply common properties
        /// </summary>
        /// <param name="control">The Xml node that contains our control properties</param>
        /// <param name="customControl">The custom control that will be created</param>
        public override void BuildControl(System.Xml.XmlNode control, System.Windows.Forms.Control customControl)
        {
            base.BuildControl(control, customControl);
        }
    }
}
